package com.handle;

import com.constant.ChannelMap;
import com.message.register.RpcRegisterResponse;
import com.register.RegisterInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;

/**
 * @author linyu.dai
 * @project rpc
 * @description
 * @creat 2022/11/27 16:25:29
 */
@Slf4j
public class DefaultHandle extends ChannelInboundHandlerAdapter {

    //连接断开时触发
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("连接断开 服务名{}", ChannelMap.CHANNEL_SERVER.get(ctx.channel()));
        //剔除channel 避免多次连接
        String serverName = ChannelMap.removeChannelServer(ctx.channel());
        //剔除注册表信息
        RegisterInfo.remove(serverName);
        //通知服务不可用
        ChannelMap.notifyClient(serverName);
    }

    //连接异常断开时触发
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.debug("已经异常断开异常是:{}", cause.getMessage());
    }
}
